const app = getApp()
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // tab: 'editing',
    // grouping: false,
    // groupingCourseRounds: [],
    // editing: false,
    // editingCourseRounds: [],
    // finish: false,
    // finishCourseRounds: [],
    // cancel: false,
    // cancelCourseRounds: []
    tab: 'ready',
    prepare: false,
    prepareCourseRounds: [],
    ready: false,
    readyCourseRounds: [],
    finish: false,
    finishCourseRounds: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      mobile: wx.getStorageSync(storage.keys.mobile)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      // grouping: false,
      // groupingCourseRounds: [],
      // editing: false,
      // editingCourseRounds: [],
      // finish: false,
      // finishCourseRounds: [],
      // cancel: false,
      // cancelCourseRounds: []
      prepare: false,
      prepareCourseRounds: [],
      ready: false,
      readyCourseRounds: [],
      finish: false,
      finishCourseRounds: []
    })
    this.list();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onSwitchTab: function (e) {
    if (this.data.tab != e.currentTarget.dataset.tab) {
      this.setData({
        tab: e.currentTarget.dataset.tab
      });
      if (!e.currentTarget.dataset.listed) {
        this.list();
      } else {
        // if (this.data.tab == 'grouping') {
        //   this.setData({
        //     courseRounds: this.data.groupingCourseRounds
        //   })
        // } else if (this.data.tab == 'editing') {
        //   this.setData({
        //     courseRounds: this.data.editingCourseRounds
        //   })
        // } else if (this.data.tab == 'finish') {
        //   this.setData({
        //     courseRounds: this.data.finishCourseRounds
        //   })
        // } else if (this.data.tab == 'cancel') {
        //   this.setData({
        //     courseRounds: this.data.cancelCourseRounds
        //   })
        // }
        if (this.data.tab == 'prepare') {
          this.setData({
            courseRounds: this.data.prepareCourseRounds
          })
        } else if (this.data.tab == 'ready') {
          this.setData({
            courseRounds: this.data.readyCourseRounds
          })
        } else if (this.data.tab == 'finish') {
          this.setData({
            courseRounds: this.data.finishCourseRounds
          })
        }
      }
    }
  },

  list: function () {
    wx.showLoading({
      title: "正在加载中..."
    })

    var that = this;

    wx.request({
      url: that.data.host + '/coach/course-rounds?mobile=' + that.data.mobile + '&status=' + that.data.tab,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            courseRounds: res.data.data.courseRounds
          });

          // if(that.data.tab == 'grouping') {
          //   that.setData({
          //     grouping: true,
          //     groupingCourseRounds: res.data.data.courseRounds
          //   })
          // } else if(that.data.tab == 'editing') {
          //   that.setData({
          //     editing: true,
          //     editingCourseRounds: res.data.data.courseRounds
          //   })
          // } else if(that.data.tab == 'finish') {
          //   that.setData({
          //     finish: true,
          //     finishCourseRounds: res.data.data.courseRounds
          //   })
          // } else if(that.data.tab == 'cancel') {
          //   that.setData({
          //     cancel: true,
          //     cancelCourseRounds: res.data.data.courseRounds
          //   })
          // }
          if (that.data.tab == 'prepare') {
            that.setData({
              prepare: true,
              prepareCourseRounds: res.data.data.courseRounds
            })
          } else if (that.data.tab == 'ready') {
            that.setData({
              ready: true,
              readyCourseRounds: res.data.data.courseRounds
            })
          } else if (that.data.tab == 'finish') {
            that.setData({
              finish: true,
              finishCourseRounds: res.data.data.courseRounds
            })
          }

          wx.hideLoading();
        } else {
          wx.hideLoading();

          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  }
})